package org.bluweb.g4wicket.wave;

import java.util.HashMap;
import java.util.Map;

import org.bluweb.g4wicket.G4WicketComponent;


@SuppressWarnings("serial")
public class Wave extends G4WicketComponent {

	private Map<String,String> options = new HashMap<String, String>();
	private String waveUrl;
	
	public Wave( String id, String waveUrl ) {
		super( id );
		this.waveUrl = waveUrl;
	}
	
	public Wave setShowHeader( boolean show ) {
		return this.setOption( "header", String.valueOf( show ) );
	}
	
	public Wave setShowFooter( boolean show ) {
		return this.setOption( "footer", String.valueOf( show ) );
	}

	public Wave setShowToolbar( boolean show ) {
		return this.setOption( "toolbar", String.valueOf( show ) );
	}
	
	private Wave setOption( String name, String value ) {
		this.options.put( name, value );
		return this;
	}

	@Override
	protected void onBeforeRender() {
		super.onBeforeRender();
		
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "id", this.getId() );
		parameters.put( "url", this.waveUrl );
		
		StringBuilder builder = new StringBuilder();
		for( String name : options.keySet() ) {
			builder.append( ',' );
			builder.append( name ).append( ":" );
			builder.append( options.get( name ) );
		}
		
		parameters.put( "options", builder.toString() );
		
		this.setJavaScriptTemplate( "Wave.js", true, parameters );
	}
}
